package cn.yunyichina.provider.order.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.yunyichina.provider.iface.entity.base.OrderExtVo;
import cn.yunyichina.provider.iface.entity.bone.OrderBoneExtendVo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.order.dao.OrderDao;
import cn.yunyichina.utils.convert.JsonUtils;

@Service("boneOrderServiceImpl")
public class BoneOrderServiceImpl {
    private Logger logger = LoggerFactory.getLogger(BoneOrderServiceImpl.class);

    @Autowired
    private OrderDao orderDao;

    /**
     * yyt.order.bone.order.list
     * 
     * @param data
     * @return
     */
    public Response listBoneOrder(String data) {
        logger.info("骨科订单查询列表, 入参data=" + data);
        Response response = new Response();
        Map<String, String> remap = new HashMap<String, String>();
        try {
            OrderExtVo boneVo = JsonUtils.parseObject(data, OrderExtVo.class);
            OrderBoneExtendVo boneExtVo = JsonUtils.parseObject(data, OrderBoneExtendVo.class);

            Map<String, Object> map = new HashMap<String, Object>();


            if(StringUtils.isNotBlank(boneVo.getId())) {
                map.put("id", boneVo.getId());
            }
            if(StringUtils.isNotBlank(boneVo.getOpenId())) {
                map.put("openId", boneVo.getOpenId());
            }
            if(StringUtils.isNotBlank(boneVo.getUserId())) {
                map.put("userId", boneVo.getUserId());
            }
            if(StringUtils.isNotBlank(boneExtVo.getProfessorDoctorId())) {
                map.put("professorDoctorId", boneExtVo.getProfessorDoctorId());
            }
            if(StringUtils.isNotBlank(boneExtVo.getAppointmentDoctorId())) {
                map.put("appointmentDoctorId", boneExtVo.getAppointmentDoctorId());  
            }
            if(StringUtils.isNotBlank(boneExtVo.getSurgeryFinishTime())) {
                map.put("surgeryFinishTime", boneExtVo.getSurgeryFinishTime());
            }
            
            if (boneVo.getPageSize() != null) {
                map.put("pageSize", boneVo.getPageSize().intValue());
            }
            if (boneVo.getPageNum() != null) {
                map.put("pageNum", boneVo.getPageNum());
            }

            if(StringUtils.isNotBlank(boneVo.getUserName())) {
                map.put("userName", boneVo.getUserName());
            }
            if(StringUtils.isNotBlank(boneExtVo.getProfessorDoctorName())) {
                map.put("professorDoctorName",boneExtVo.getProfessorDoctorName());
            }
            if(StringUtils.isNotBlank(boneExtVo.getAppointmentDoctorName())) {
                map.put("appointmentDoctorName", boneExtVo.getAppointmentDoctorName());
            }
            if(StringUtils.isNotBlank(boneVo.getEndCreatedTime())){
                map.put("endCreateTime", boneVo.getEndCreatedTime());
            }
            if(StringUtils.isNotBlank(boneVo.getBeginCreatedTime())){
                map.put("beginCreateTime", boneVo.getBeginCreatedTime());
            }
            if(boneVo.getOrderStatus() != null){
                map.put("orderStatus",boneVo.getOrderStatus());
            }
            if(boneVo.getOrderType() !=null) {
                if(boneVo.getOrderType().intValue() != 0) {
                    map.put("isNotQueryDeposit", "true");
                    map.put("orderType",boneVo.getOrderType());
                }
            } else {
                map.put("isNotQueryDeposit", "true");
            }
            
            if(StringUtils.isNotBlank(boneVo.getOrderNo())) {
                map.put("orderNo", boneVo.getOrderNo());
                map.put("isNotQueryDeposit", null);
            }
            if(boneVo.getPayStatus() !=null) {
                map.put("payStatus",boneVo.getPayStatus());
            }


            logger.info("骨科订单查询列表, 查询入参request=" + JSON.toJSONString(map));
            List<Map<String, Object>> datas = orderDao.selectBoneOrder(map);
            logger.info("骨科订单查询列表, 查询返回response=" + JsonUtils.toJsonString(data));

            remap.put("datas", JsonUtils.toJsonString(datas));
            
            logger.info("骨科订单查询列表, 查询订单总数入参request=" + JSON.toJSONString(map));
            long datasCount = orderDao.selectBoneOrderCount(map);
            logger.info("骨科订单查询列表, 查询订单总数返回response=" + datasCount);
            remap.put("datasCount", String.valueOf(datasCount));
            response.setResult(JsonUtils.toJsonString(remap));
            
            return response;
        } catch (Exception e) {
            logger.error("骨科订单查询列表报错", e);
            return Response.failure();
        }
    }


    /**
     * 根据用户id查询骨科订单
     * @interfaceName: yyt.order.bone.order.and.order.for.userid.get
     * @param data
     * @return
     */
    public Response getBoneOrderAndOrderForUserid(String data){
        logger.info("根据用户id查询骨科订单, 入参data=" + JsonUtils.toJsonString(data));
        Response response = new Response();
        OrderExtVo boneVo = JsonUtils.parseObject(data, OrderExtVo.class);
        if(StringUtils.isBlank(boneVo.getUserId())){
            response.setResultCodeAndMessage(Response.FAILURE_CODE , "入参 user_id 为空！");
        }

        List<Map<String, Object>> maps = orderDao.selectBoneOrderAndOrderForUserId(boneVo.getUserId());

        if(maps.size() > 0){
            response.setResultCodeAndMessageAndResult(Response.SUCCESS_CODE, Response.SUCCESS_MESSAGE , JsonUtils.toJsonString(maps));
        }else{
            response.setResultCodeAndMessageAndResult(Response.FAILURE_CODE, "查无结果" , null);
        }

        return response;
    }



    /**
     * 根据医生(专家或主治)id查询骨科订单
     * @interfaceName: yyt.order.bone.order.and.order.for.doctorid.get
     * @param data
     * @return
     */
    public Response getBoneOrderAndOrderForDoctorid(String data){
        logger.info("根据医生(专家或主治)id查询骨科订单, 入参data=" + JsonUtils.toJsonString(data));
        Response response = new Response();
        Map<String,String> map = JsonUtils.parseObject(data, Map.class);

        String doctorId = map.get("doctor_id");
        String doctorType = map.get("doctor_type"); // 1.专家  2.主治

        if(StringUtils.isBlank(doctorId) || StringUtils.isBlank(doctorType)){
            response.setResultCodeAndMessage(Response.FAILURE_CODE , "入参 doctor_id 、doctor_type 为空！");
        }

        List<Map<String, Object>> maps = orderDao.selectBoneOrderAndOrderForDoctorId(doctorId , doctorType);

        if(maps.size() > 0){
            response.setResultCodeAndMessageAndResult(Response.SUCCESS_CODE, Response.SUCCESS_MESSAGE , JsonUtils.toJsonString(maps));
        }else{
            response.setResultCodeAndMessageAndResult(Response.FAILURE_CODE, "查无结果" , null);
        }

        return response;
    }


    
    /**
     * yyt.order.bone.order.get
     * 
     * @param data
     * @return
     */
    public Response getBoneOrder(String data) {
        logger.info("获取骨科订单信息, 入参data=" + data);
        Response response = new Response();
        try {
            OrderExtVo boneVo = JsonUtils.parseObject(data, OrderExtVo.class);

            Map<String, String> map = new HashMap<String, String>();
            map.put("orderNo", boneVo.getOrderNo());
            logger.info("获取骨科订单信息, 查询入参request: " + JSON.toJSONString(map));
            Map<String, Object> datas = orderDao.getBoneOrder(map);
            logger.info("获取骨科订单信息, 查询返回response: " + JsonUtils.toJsonString(datas));
            response.setResult(JsonUtils.toJsonString(datas));
            
            return response;
        } catch (Exception e) {
            logger.error("获取骨科订单信息报错", e);
            return Response.failure();
        }
    }
    
    /**
     * yyt.order.bone.order.operation.by.deposit.order.no.get
     * 
     * @param data
     * @return
     */
    public Response getBoneOrderOperationByDepositOrderNo(String data) {
        logger.info("根据押金单订单号查询手术单信息, 入参data: " + data);
        Response response = new Response();
        try {
            OrderExtVo boneVo = JsonUtils.parseObject(data, OrderExtVo.class);

            Map<String, String> map = new HashMap<String, String>();
            map.put("orderNo", boneVo.getOrderNo());
            logger.info("根据押金单订单号查询手术单信息, 查询入参request: " + JsonUtils.toJsonString(map));
            Map<String, Object> datas = orderDao.getBoneOperationOrderByDepositOrderNo(map);
            logger.info("根据押金单订单号查询手术单信息, 查询返回response: " + JsonUtils.toJsonString(datas));
            response.setResult(JsonUtils.toJsonString(datas));
            
            return response;
        } catch (Exception e) {
            logger.error("根据押金单订单号查询手术单信息报错", e);
            return Response.failure();
        }
    }
}
